// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas.isoregion.jobs;

import java.util.concurrent.ConcurrentLinkedQueue;
import zombie.core.Core;
import zombie.core.raknet.UdpConnection;

public final class RegionJobManager {
    private static final ConcurrentLinkedQueue<JobSquareUpdate> poolSquareUpdate = new ConcurrentLinkedQueue();
    private static final ConcurrentLinkedQueue<JobChunkUpdate> poolChunkUpdate = new ConcurrentLinkedQueue();
    private static final ConcurrentLinkedQueue<JobApplyChanges> poolApplyChanges = new ConcurrentLinkedQueue();
    private static final ConcurrentLinkedQueue<JobServerSendFullData> poolServerSendFullData = new ConcurrentLinkedQueue();
    private static final ConcurrentLinkedQueue<JobDebugResetAllData> poolDebugResetAllData = new ConcurrentLinkedQueue();

    public static JobSquareUpdate allocSquareUpdate(int x, int y, int z, byte flags) {
        JobSquareUpdate jobSquareUpdate = (JobSquareUpdate)poolSquareUpdate.poll();
        if (jobSquareUpdate == null) {
            jobSquareUpdate = new JobSquareUpdate();
        }

        jobSquareUpdate.worldSquareX = x;
        jobSquareUpdate.worldSquareY = y;
        jobSquareUpdate.worldSquareZ = z;
        jobSquareUpdate.newSquareFlags = flags;
        return jobSquareUpdate;
    }

    public static JobChunkUpdate allocChunkUpdate() {
        JobChunkUpdate jobChunkUpdate = (JobChunkUpdate)poolChunkUpdate.poll();
        if (jobChunkUpdate == null) {
            jobChunkUpdate = new JobChunkUpdate();
        }

        return jobChunkUpdate;
    }

    public static JobApplyChanges allocApplyChanges(boolean saveToDisk) {
        JobApplyChanges jobApplyChanges = (JobApplyChanges)poolApplyChanges.poll();
        if (jobApplyChanges == null) {
            jobApplyChanges = new JobApplyChanges();
        }

        jobApplyChanges.saveToDisk = saveToDisk;
        return jobApplyChanges;
    }

    public static JobServerSendFullData allocServerSendFullData(UdpConnection conn) {
        JobServerSendFullData jobServerSendFullData = (JobServerSendFullData)poolServerSendFullData.poll();
        if (jobServerSendFullData == null) {
            jobServerSendFullData = new JobServerSendFullData();
        }

        jobServerSendFullData.targetConn = conn;
        return jobServerSendFullData;
    }

    public static JobDebugResetAllData allocDebugResetAllData() {
        JobDebugResetAllData jobDebugResetAllData = (JobDebugResetAllData)poolDebugResetAllData.poll();
        if (jobDebugResetAllData == null) {
            jobDebugResetAllData = new JobDebugResetAllData();
        }

        return jobDebugResetAllData;
    }

    public static void release(RegionJob job) {
        job.reset();
        switch (job.getJobType()) {
            case SquareUpdate:
                poolSquareUpdate.add((JobSquareUpdate)job);
                break;
            case ApplyChanges:
                poolApplyChanges.add((JobApplyChanges)job);
                break;
            case ChunkUpdate:
                poolChunkUpdate.add((JobChunkUpdate)job);
                break;
            case ServerSendFullData:
                poolServerSendFullData.add((JobServerSendFullData)job);
                break;
            case DebugResetAllData:
                poolDebugResetAllData.add((JobDebugResetAllData)job);
                break;
            default:
                if (Core.bDebug) {
                    throw new RuntimeException("No pooling for this job type?");
                }
        }
    }
}
